package com.bestsonic.view.controller;

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bestsonic.domain.Page;
import com.bestsonic.domain.WebPage;
import com.bestsonic.mapper.KeywordMapper;
import com.bestsonic.search.utils.RankedList;
import com.bestsonic.spider.utils.DBUtils;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Controller
public class SearchController {

	@RequestMapping("/getData")
	@ResponseBody
	public JSONObject getDate(@RequestParam("searchText") String searchText, @RequestParam("size") Integer size, @RequestParam("pageNum") Integer pageNum){
		Page<WebPage> page = RankedList.rankedList(searchText.split(" "), pageNum, size);
		JSONObject object = JSONObject.fromObject(page);
		return object;
	}
	
	@RequestMapping("/getKeyword")
	@ResponseBody
	public JSONArray getKeyword(){
		SqlSession session = DBUtils.getSession();
		KeywordMapper mapper = session.getMapper(KeywordMapper.class);
		List<String> list = mapper.selectKeywords();
		JSONArray object = JSONArray.fromObject(list);
		return object;
	}
}
